import * as glob from 'glob';
import fsextra from 'fs-extra';
const { removeSync, copyFileSync, readFileSync, writeFileSync, mkdirSync } = fsextra;
import { resolve, parse, dirname } from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// 先删除server
removeSync(resolve(__dirname, './server'));
mkdirSync(resolve(__dirname, './server'));

const serverlibs = [];
glob.globSync("../jdec.jdt.ls.decompiler/*/target/*-SNAPSHOT.jar").forEach(file0 => {
    //  path ---为加载的每个index.js文件：./src/reva_demo/module/apply/list/index.js
    // chunk = 节点+list/card: reva_demo/module/apply/list
    const file1 = resolve(file0);

    const serverlibname = './server/' + parse(file1).name + parse(file1).ext;
    serverlibs.push(serverlibname);
    copyFileSync(file1, resolve(__dirname, serverlibname));
});


const packagejson = readFileSync(resolve(__dirname, 'package.json'), 'utf8');
const packageObj = JSON.parse(packagejson);
serverlibs.sort();
packageObj.contributes.javaExtensions = serverlibs;

writeFileSync(resolve(__dirname, 'package.json'), JSON.stringify(packageObj, null, "\t"), 'utf8');
console.log('写入package.json/contributes/javaExtensions', serverlibs);
